package ${bussPackage}.${entityPackage}.service;

import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.chrhc.projects.cpub.back.common.utils.UtilString;
import com.chrhc.projects.cpub.back.common.utils.UtilDate;
import java.util.Date;
import java.util.Map;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.Page;
import com.chrhc.projects.cpub.back.common.CpubPage;
import java.util.UUID;
import com.chrhc.projects.cpub.back.common.CommonService;
import com.chrhc.projects.uiap.common.persistence.entity.CurrentUser;
//import com.chrhc.projects.uiap.common.utils.ShiroUtils;
import com.chrhc.projects.cpub.back.common.utils.UtilUuid;
import ${bussPackage}.${entityPackage}.dao.${className}Mapper;
import ${bussPackage}.${entityPackage}.model.${className};
import ${bussPackage}.${entityPackage}.model.${className}Example;


/**
 * <一句话功能简述${className}ServiceI>
 * <功能详细描述>
 * @author 姓名${author}
 * @version [${version}, ${curDate?string("yyyy年MM月dd日")}]
 */
@Service("${lowerName}Service")
public class ${className}ServiceImpl extends CommonService implements ${className}ServiceI{
	private final static Logger log= Logger.getLogger(${className}ServiceImpl.class);
	

	

	@Autowired
    private ${className}Mapper ${lowerName}Mapper;

	
	/**
    * <查询单条>
    * <根据主键查询>
    * @see #get(String)
    * @param id
    * @return ${className}
    */
	public ${className} get(String id) {
		return ${lowerName}Mapper.selectByPrimaryKey(id);
	}

	
	
	/**
    * <查询全部>
    * <根据查询条件查询>
    * @see #findAll
    * @param parameters
    * @return List<${className}>
    */
	public List<${className}> findAll(Map<String, Object> parameters) {

		${className}Example example = putParameter(parameters);
			//开始拼条件
		return ${lowerName}Mapper.selectByExample(example);
			
	}
	
	/**
    * <保存方法>
    * <根据主键是否存在判断是新增还是更新>
    * @see #save(${className})
    * @param entity
    * @return ${className}
    */
	@Transactional
	public ${className} save(${className} entity,CurrentUser user){
		if(entity.getId()==null||UtilString.isBlank(entity.getId().toString())){
			add(entity,user);
		}else{
		    update(entity,user);
		}
		
		return entity;
	}
	
	/**
    * <普通删除>
    * <根据主键删除一条数据>
    * @see #delete(String)
    * @param primaryKey
    */
	public void delete(String primaryKey){
		if(primaryKey == null || primaryKey.equals("")){
			return;
		}
		//${lowerName}Mapper.deleteByPrimaryKey(primaryKey);
		
		 ${className} record = new ${className}();
        record.setId(primaryKey);
        record.setIsDeleted(1);
        ${lowerName}Mapper.updateByPrimaryKeySelective(record);
	}
	
	/**
    * <批量删除>
    * <根据主键数组删除一批数据>
    * @see #deleteByIds(String[])
    * @param ids
    */ 
	@Transactional
	public void deleteByIds(String... ids){
		if(ids == null || ids.length < 1){
			return;
		}
		for(String id : ids ){
			delete(id);
		}
	}
	
	
	public void add(${className} entity,CurrentUser user) {
		//设置主键.字符类型采用UUID,数字类型采用自增
		entity.setId(UtilUuid.getUUid32());
		Date curDate = new Date();
        if(user!=null){
	        entity.setCreateUserId(user.getLoginName());
	        entity.setCreateUserName(user.getName());
	        entity.setCreateTime(curDate);
	        entity.setUpdateUserId(user.getLoginName());
	        entity.setUpdateUserName(user.getName());
	        entity.setUpdateTime(curDate);
        }
		${lowerName}Mapper.insertSelective(entity);//默认情况选择性插入吧
	}
	
	public void update(${className} entity,CurrentUser user)  {
		//${lowerName}Mapper.update(t);
		 Date curDate = new Date();
        if(user!=null){
	        entity.setUpdateUserId(user.getLoginName());
	        entity.setUpdateUserName(user.getName());
	        entity.setUpdateTime(curDate);
        }
		${lowerName}Mapper.updateByPrimaryKeySelective(entity);
	}
	
	
	public void updateBySelective(${className} entity, ${className}Example example,CurrentUser user){
		 Date curDate = new Date();
        if(user!=null){
	        entity.setUpdateUserId(user.getLoginName());
	        entity.setUpdateUserName(user.getName());
	        entity.setUpdateTime(curDate);
        }
		${lowerName}Mapper.updateByExampleSelective(entity,example);
	}
	
	
	@Override
    public List<${className}> selectPageDataByParameters(Map<String, Object> parameters)
    {
        return findAll(parameters);
    }
    
    /**
     * 本页面设置参数
     * 
     * @param parameters
     * @return
     */
    private ${className}Example putParameter(Map<String, Object> parameters)
    {
        ${className}Example example = new ${className}Example();
        ${className}Example.Criteria c = example.createCriteria();
         c.andIsDeletedEqualTo(0);
        if (parameters != null)
        {
            /*c.andBizNameLike("%" + UtilString.null2blank(parameters.get("bizName")) + "%");
            String bizType = UtilString.null2blank(parameters.get("bizType"));
            if (!UtilString.isBlank(bizType))
            {
                c.andBizTypeEqualTo(bizType);
            }*/
            
            String d1 = UtilString.null2blank(parameters.get("startTime"));
            String d2 = UtilString.null2blank(parameters.get("endTime"));
            Date value1 = UtilDate.parseDate(d1, "yyyy-MM-dd");
            Date value2 = UtilDate.getDateDiffDays(UtilDate.parseDate(d2, "yyyy-MM-dd"), 1);
            c.andCreateTimeBetween(value1, value2);
        }
        return example;
    }
	
	
	/*public int queryByCount(BasePage page)throws Exception{
		return ${lowerName}Mapper.queryByCount(page);
	}
	
	public List<T> queryByList(BasePage page) throws Exception{
		Integer rowCount = queryByCount(page);
		page.getPager().setRowCount(rowCount);
		return ${lowerName}Mapper.queryByList(page);
	}*/
	

}
